﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using HF.BizWinner.DB.Linq;

public partial class Mapview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public Dictionary<string, object> JsonConvert(string json)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        object jsonobject = jss.DeserializeObject(json);
        return (Dictionary<string, object>)jsonobject;
    }

    protected void acbCenter_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {
        Dictionary<string, object> d = JsonConvert(e.Parameter);
        int level = int.Parse(d["Level"].ToString());
        
        string result = string.Empty;
        BizWinnerDataContext ctx = new BizWinnerDataContext();

        if (level == 1)
        {
            var rtns = from rtn in ctx.Region
                       where rtn.R_Id == d["RID"].ToString()
                       select rtn;
            if (rtns.Count() <= 0) result = "";
            else
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                result = jss.Serialize(rtns.First());
            }
        }

        if (level == 2)
        {
            var rtns = from rtn in ctx.Province
                       where rtn.P_Id == d["PID"].ToString()
                       select rtn;
            if (rtns.Count() <= 0) result = "";
            else
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                result = jss.Serialize(rtns.First());
            }
        }

        if (level == 3)
        {
            var rtns = from rtn in ctx.City
                       where rtn.C_Id == d["CID"].ToString()
                       select rtn;
            if (rtns.Count() <= 0) result = "";
            else
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                result = jss.Serialize(rtns.First());
            }
        }

        if (level == 4)
        {
            var rtns = from rtn in ctx.District
                       where rtn.D_Id == d["DID"].ToString()
                       select rtn;
            if (rtns.Count() <= 0) result = "";
            else
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                result = jss.Serialize(rtns.First());
            }
        }

        if (level == 5)
        {
            var rtns = from rtn in ctx.Street
                       where rtn.S_Id == d["SID"].ToString()
                       select rtn;
            if (rtns.Count() <= 0) result = "";
            else
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                result = jss.Serialize(rtns.First());
            }
        }

        e.Result = result;
    }
}
